package com.yc.system.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.yc.system.dto.RegisterInfo;
import com.yc.system.dto.UserDto;
import com.yc.system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import  com.yc.system.service.IUserService;


import com.yc.framework.baseClass.BaseController;
//import com.yc.framework.baseClass.ServiceFacade;
import com.yc.framework.baseClass.R;
import com.yc.system.entity.User;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
    * 用户表 前端控制器
    * </p>
 *
 * @author 1850140986@qq.com
 * @since 2022-12-03
 */
@RestController
//@RequestMapping("/sys/user")
public class UsersController extends BaseController
    {

		@Value("${file.upload.path}")
		private String path;
		@Value("${file.upload.relativePath}")
		private String relativePath;

    @Autowired
  private IUserService service;

    @Autowired
		UserMapper userMapper;

  /**
   * 根据条件分页查询User
   * @param model 实体对象
   * @return 查询列表
   */
     @PostMapping("/sys/user/list")
    public R list(@RequestBody User model){
        return service.list(model);

    }

    /**
    * 根据主键查询单个对象
    * @param model 实体对象包含uuid
    * @return 成功或失败
    */
    @GetMapping("/sys/user/getById/{id}")
    public R getById(@PathVariable String id) {
        return service.getModelById(id);
    }

    /**
	 * 新增实体对象
	 *
     * @param model 实体对象
	 * @return 新增是否成功
	 */
     @PostMapping("/sys/user/save")
	public R save(@RequestBody User model) {
		return service.saveModel(model);

	}

    /**
	 * 批量新增实体对象
	 *
     * @param modelList 实体对象列表
	 * @return 新增是否成功
	 */
	@PostMapping("/sys/user/saveBatch")
	public R saveBatch(@RequestBody List<User> modelList) {
		return service.saveModelBatch(modelList);

	}

    /**
	 * 根据主键删除实体
	 *
     * @param uuid 实体携带主键
	 * @return 删除是否成功
	 */
	@DeleteMapping("/sys/user/removeById/{id}")
	public R removeById(@PathVariable String id) {
		return service.removeModelById(id);
	}

     /**
	 * 根据主键列表删除实体
	 *
     * @param idList 实体主键
	 * @return 删除是否成功

	@DeleteMapping("/removeByIdList")
	public R removeByIdList(@RequestBody List<User> idList) {
        return service.removeModelByIdList(idList);

    }
 */
    /**
    * 根据主键修改实体
    *
    * @param model 等待修改的对象
    * @return 修改是否成功
    */
    @PutMapping("/sys/user/updateById")
    public R updateModelById(@RequestBody User model) {
        return service.updateModelById(model);

    }

    /**
    * 根据主键列表修改实体列表
    *
    * @param modelList 等待修改的对象列表
    * @return 修改是否成功
    */
    @PostMapping("/sys/user/updateByIdBatch")
    public R updateModelByIdBatch(@RequestBody List<User> modelList) {
        return service.updateModelByIdBatch(modelList);

    }

      /**
	 * 根据多主键列表删除实体
	 *
     * @param idList 多主键
	 * @return 删除是否成功
	 */
	@DeleteMapping("/sys/user/removeByIds/{ids}")
	public R removeByIdList(@PathVariable String ids) {
        return service.removeModelByIds(ids);
    }

    @GetMapping("/sys/user/getUserByUserId/{userId}")
	public R getUserByUserId(@PathVariable int userId) {
		return service.getUserByUserId(userId);
	}

	@PostMapping("index/sys/user/changePpictureAddr/{userId}")
	public R changePpictureAddr(MultipartFile file, @PathVariable int userId){
		try {
			if (null != file) {
				String originalFileName = file.getOriginalFilename();//原始文件名
				if(originalFileName==null){return R.err("文件名错误！");}
				String name = String.format("%s%s", UUID.randomUUID().toString().toLowerCase(),
						originalFileName.substring(originalFileName.lastIndexOf(".")));
				String path = "/ppictureAddr/";
				String pathName = path + name;
				String realPath = this.path + path;
				String reqFullFileName = this.relativePath + pathName;
				File dir = new File(realPath);
				if (!dir.exists()) {
					dir.mkdirs();
				}
				File f = new File(realPath, name);
				file.transferTo(f);//写入文件

//                业务逻辑
				//  返回bookId以及coverPhoto
				return service.changePpictureAddr(userId,reqFullFileName);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.err("上传失败");

	}

	//注册平台用户
	@PostMapping("index/sys/user/register")
	public R register(@RequestBody RegisterInfo registerInfo){
		return service.register(registerInfo);
	}

	@GetMapping("index/sys/user/vilUsername")
		public R vilUsername(@RequestParam("username") String username){
		Long userIdByUsername=userMapper.getUserIdByUsername(username);
		if(userIdByUsername!=null){
			return R.err().put("data","该用户名已被注册！");
		}else{
			return R.ok();
		}
	}

		@PostMapping("sys/user/updataByUserInfo")
		public R updataByUserInfo(@RequestBody User user) {
			return service.updataByUserInfo(user);
		}

		@PostMapping("checkAdminByUserId")
		public R checkAdminByUserId(@RequestBody User user) {
			return service.checkAdminByUserId(user.getUserId());
		}


}
